﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Utils.Extention
{
    public static class Extensions_Objec
    {
        public static double ToLimsDouble(this object s, int? digits = null)
        {
            if (s == null || s == DBNull.Value)
                return 0d;

            double.TryParse(s.ToString(), out double result);

            if (digits == null)
                return result;

            return Math.Round(result, digits.Value);
        }

        public static DateTime ToLimsCnDate(this DateTime utcTime)
        {
            try
            {
                // 获取中国时区的信息
                TimeZoneInfo chinaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");

                // 将 UTC 时间转换为中国本地时间
                DateTime chinaTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, chinaTimeZone);
                return chinaTime;
            }
            catch (Exception)
            {
                return utcTime;
            }
        }


        public static List<string> ToLimsStrList(this string[] s, int? digits = null)
        {
            if (s == null || s.Count() <= 0)
                return new List<string>();

            try
            {
                return s.ToList();
            }
            catch (Exception)
            {
                return new List<string>();
            }


        }

        public static long ToClLong(this object? value, long defaultValue = 0L)
        {
            if (value == null)
                return defaultValue;
            long.TryParse(value.ToString(), out defaultValue);
            return defaultValue;
        }

        public static int ToClInt(this object? value, int defaultValue = 0)
        {
            if (value == null)
                return defaultValue;
            int.TryParse(value.ToString(), out defaultValue);
            return defaultValue;
        }
    }
}
